<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Discuss;
use App\Http\Requests;
use App\Message;
use App\Parsedown;
use App\Study;
use App\Studycomment;
use App\Task;
use App\User;
use App\Utils;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;
use DateTime;

class comController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
	 public function del(Request $request)
    {
        $input = $request->all();
        $comment = Comment::find($input["id"]);
		if(Auth::user()->id!=$comment->userid&&Auth::user()->id!=19)return;
        $comment->delete = 1;
        $comment->save();
        echo "删除成功 ";
    }

    public function comment(Request $request)
    {
        /*type=0 题目的评论
         *    =1 回答
         *    =2 学习资料的评论
         *    =3 回答的评论
         */
        $input = $request->all();
        if ($input['type'] == '3') {
			$input['id']=Utils::url2id($input['id']);
            $data = array();
            $data['type'] = $input['type'];
            $data['taskid'] = $input['id'];
            $data['comment'] = $input['content'];
            if (isset($input['ruserid'])) $data['ruserid'] = Utils::url2id($input['ruserid']);
            $data['userid'] = Auth::user()->id;
            $comment=Comment::create($data);

            $com = Comment::find($input['id']);
            $dis = Discuss::find($com->taskid);
			$dis->rtime=time();
			$dis->save();
            $data = array();
            $data['senderid'] = 1;
            if (isset($input['ruserid']))
                $data['receiverid'] = Utils::url2id($input['ruserid']);
            else {
                $data['receiverid'] = $com->userid;
            }
            $receiver = User::find($data['receiverid']);
            $receiver->msgcnt += 1;
            $receiver->save();
            $ended = true;
            if ($dis->end_time != null) {
                $now = new DateTime();
                $end = new DateTime($dis->end_time);
                if ($dis->tag == 'topic' && $now < $end) {
                    $ended = false;
                }
            }
            if (!$ended && Auth::user()->id == $com->userid) {
                $name = "(作者)";
            } else {
                $name = Auth::user()->name;
            }

            $data['content'] = $name . " 评论了你在『" . $dis->title . "』中的回答，<a target='_blank' href='/q/" . Utils::id2url($dis->id)."#r".Utils::id2url($input['id'])."'>点击查看</a>";

            Message::create($data);
        } else if ($input['type'] == '2') {
            $arr = array();
            $arr['userid'] = Auth::user()->id;
            $arr['taskid'] = $input['id'];
            $arr['comment'] = $input['content'];
            Studycomment::create($arr);

            $data = array();
            $data['senderid'] = 1;
            $study = Study::find($input['id']);
            $task = Task::find($study->taskid);
            $ruser = User::find($study->userid);
            $ruser->msgcnt += 1;
            $ruser->save();
            $data['receiverid'] = $ruser->id;
            $Parsedown = new Parsedown();
            //$data['content']=$Parsedown->text($input['content']);
            $content = "**" . Auth::user()->name . "**评论了**" . $task['title'] . '**中你的学习资料，<a target="_blank" href="/task/' . Utils::id2url($task->id) . "/learn#" . Utils::id2url($input['id']) . '">点击查看</a>。';
            $data['content'] = $Parsedown->text($content);
            Message::create($data);

        }
            $user = User::find($comment->userid);
            $comment->time=$comment->created_at->format("Y-m-d");
            $comment->user = User::select("id","name","score","solveNum","rank","introduce")->find($comment->userid);
            $comment->user->id=Utils::id2url($comment->user->id);
            $comment->user->url = $user->avatar->url('thumb');
            $comment->ruser = User::select("id","name","score","solveNum","rank","introduce")->find($comment->ruserid);
            if($comment->ruser!=null)
            {   
                $ruser = User::find($comment->ruserid);
                $comment->ruser->id=Utils::id2url($comment->ruser->id);
                $comment->ruser->url = $ruser->avatar->url('thumb');
            }   
            if(Auth::check()&&Auth::user()->id==$user->id)
            $comment->candelete=true;
            else $comment->candelete=false;
			$comment->id=Utils::id2url($comment->id);
        return response()->json($comment);

    }

}
